package com.luuyi.actionchain;

import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.luuyi.actionchain.action.Action;
import com.luuyi.actionchain.action.AsyncDelayAction;
import com.luuyi.actionchain.action.DialogAction;
import com.luuyi.actionchain.action.ToastAction;

import java.util.ArrayList;

public class ActionsAdapter extends RecyclerView.Adapter<ActionsAdapter.BaseViewHolder> implements IItemHelper {

    private ArrayList<Action> actions;

    public ActionsAdapter() {
        actions = new ArrayList<>();
    }

    public void addAction(Action action) {
        if (actions.size() > 0) {
            actions.get(actions.size() - 1).setNext(action);
        }
        actions.add(action);
        notifyDataSetChanged();
    }

    public void startFirstAction() {
        if (actions.size() > 0) {
            actions.get(0).start();
        }
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        // 链表的单节点交换
        Action tempAction = actions.get(toPosition);
        actions.set(toPosition, actions.get(fromPosition));
        actions.set(fromPosition, tempAction);

        if (toPosition != 0) {
            actions.get(toPosition - 1).setNext(actions.get(toPosition));
        }
        if (fromPosition != 0) {
            actions.get(fromPosition - 1).setNext(actions.get(fromPosition));
        }
        if (toPosition != actions.size() - 1) {
            actions.get(toPosition).setNext(actions.get(toPosition + 1));
        } else {
            actions.get(toPosition).setNext(null);
        }
        if (fromPosition != actions.size() - 1) {
            actions.get(fromPosition).setNext(actions.get(fromPosition + 1));
        } else {
            actions.get(fromPosition).setNext(null);
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void removeItem(int position) {
        if (position < 0 || position >= actions.size()) {
            return;
        }
        if (position > 0 && position < actions.size() - 1) {
            actions.get(position - 1).setNext(actions.get(position + 1));
        }
        actions.remove(position);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        BaseViewHolder holder;
        switch (i) {
            case 1:
                holder = new ToastViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_toast_action, viewGroup, false));
                break;
            case 2:
                holder = new DialogViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_dialog_action, viewGroup, false));
                break;
            case 3:
                holder = new AsyncDelayViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_sleep_action, viewGroup, false));
                break;
            default:
                holder = new ToastViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_toast_action, viewGroup, false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int i) {
        viewHolder.onItemShow(i);
    }

    @Override
    public int getItemViewType(int position) {
        if (actions.get(position) instanceof ToastAction) {
            return 1;
        } else if (actions.get(position) instanceof DialogAction) {
            return 2;
        } else if (actions.get(position) instanceof AsyncDelayAction) {
            return 3;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return actions.size();
    }

    public abstract class BaseViewHolder extends RecyclerView.ViewHolder {

        protected ImageView ivDrag;
        protected ImageView ivClose;

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
            ivDrag = itemView.findViewById(R.id.iv_drag);
            ivClose = itemView.findViewById(R.id.iv_close);
        }

        public void onItemShow(int position) {
            ivClose.setOnClickListener(v -> removeItem(position));
        }
    }

    public class ToastViewHolder extends BaseViewHolder {
        private EditText etContent;

        public ToastViewHolder(@NonNull View itemView) {
            super(itemView);
            etContent = itemView.findViewById(R.id.et_content);
        }

        @Override
        public void onItemShow(int position) {
            super.onItemShow(position);
            final ToastAction action = (ToastAction) actions.get(position);
            if (!TextUtils.isEmpty(action.getContent())) {
                etContent.setText(action.getContent());
            } else {
                etContent.setText("");
            }
            etContent.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    action.setContent(s.toString());
                }
            });
        }
    }

    public class DialogViewHolder extends BaseViewHolder {
        private EditText etContent;

        public DialogViewHolder(@NonNull View itemView) {
            super(itemView);
            etContent = itemView.findViewById(R.id.et_content);
        }

        @Override
        public void onItemShow(int position) {
            super.onItemShow(position);
            final DialogAction action = (DialogAction) actions.get(position);
            if (!TextUtils.isEmpty(action.getContent())) {
                etContent.setText(action.getContent());
            }
            etContent.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    action.setContent(s.toString());
                }
            });
        }
    }

    public class AsyncDelayViewHolder extends BaseViewHolder {
        private EditText etContent;

        public AsyncDelayViewHolder(@NonNull View itemView) {
            super(itemView);
            etContent = itemView.findViewById(R.id.et_content);
        }

        @Override
        public void onItemShow(int position) {
            super.onItemShow(position);
            final AsyncDelayAction action = (AsyncDelayAction) actions.get(position);
            etContent.setText(String.valueOf(action.getDelayTime()));
            etContent.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    try {
                        action.setDelay(Long.valueOf(s.toString()));
                    } catch (NumberFormatException e) {
                        action.setDelay(AsyncDelayAction.DEFAULT_DELAY_TIME);
                    }
                }
            });
        }
    }
}
